package com.xl.base.aop;

import java.lang.reflect.Method;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.reflect.MethodSignature;

import com.xl.base.annotation.DataSource;
import com.xl.base.dbsource.DynamicDataSourceHolder;
import com.xl.core.enums.SourceEnum;

/**
 * 切面：用于判断使用的数据源
 * 
 * @author 梁华
 * 
 */
public class DataSourceAspect {
	public void before(JoinPoint point) {
		try {
			Method m = ((MethodSignature) point.getSignature()).getMethod();
			if (m != null && m.isAnnotationPresent(DataSource.class)) {
				DataSource data = m.getAnnotation(DataSource.class);
				DynamicDataSourceHolder.putDataSource(data.source());
				System.out.println("******************************" + data.source().getDesc() + "*********");
			} else {
				DynamicDataSourceHolder.putDataSource(SourceEnum.TYPE_MASTER);
				System.out.println("******************************主库*********");
			}
		} catch (Exception e) {
			DynamicDataSourceHolder.putDataSource(SourceEnum.TYPE_MASTER);
		}
	}
}
